#pragma once

#include "FrogProgressReportDialog.h"
#include "..\FilerFrogUtils\FilerFrogUtils.h"

// CFrogRenameAutoAlphaAppendDialog
template <class T>
class CFrogOrderFilesDialog: public CFrogProgressReportDialog<T>
{
public:
	list<wstring> m_OutFilesList;

	CFrogOrderFilesDialog(HICON hIcon):
	CFrogProgressReportDialog(hIcon)
	{
		himlFilesIcons = NULL;
		iconsList.clear();
	}

	virtual ~CFrogOrderFilesDialog()
	{
		for (list<HICON>::iterator iter = iconsList.begin(); iter != iconsList.end();  iter++)
		{
			if (*iter != NULL)
			{
				DestroyIcon(*iter);
			}
		}

		if (himlFilesIcons != NULL)
		{
			ImageList_Destroy(himlFilesIcons);							
		}
	}

	BEGIN_MSG_MAP(T)
		COMMAND_HANDLER(IDC_BUTTON_UP, BN_CLICKED, OnClickedUp)
		COMMAND_HANDLER(IDC_BUTTON_DOWN, BN_CLICKED, OnClickedDown)
		// FrogBaseDialog Inherited
		CHAIN_MSG_MAP(CFrogProgressReportDialog)
	END_MSG_MAP()

	LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		CFrogBaseDialog::OnInitDialog(uMsg, wParam, lParam, bHandled);
		SetupFilesOrderList();
		return 0;
	}

	LRESULT OnClickedUp(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
	{
		bHandled = TRUE;

		HWND listFiles = GetDlgItem(IDC_LIST_FILES);
		int index = ListView_GetNextItem(listFiles, -1, LVNI_SELECTED);

		if (index > 0)
		{
			FilerFrogUtils::SwapItems(listFiles, index, index - 1);
		}

		::SetFocus(listFiles);

		return 0;
	}

	LRESULT OnClickedDown(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
	{
		bHandled = TRUE;

		HWND listFiles = GetDlgItem(IDC_LIST_FILES);
		int index = ListView_GetNextItem(listFiles, -1, LVNI_SELECTED);
		int count = ListView_GetItemCount(listFiles);

		if (index != -1 && index < count - 1)
		{
			FilerFrogUtils::SwapItems(listFiles, index, index + 1);
		}

		::SetFocus(listFiles);

		return 0;
	}

protected:
	HIMAGELIST himlFilesIcons;
	list<HICON> iconsList;

	void SetupFilesOrderList()
	{
		HWND listFiles = GetDlgItem(IDC_LIST_FILES);

		wchar_t *columnName = L"File Name";

		LVCOLUMN colData;
		ZeroMemory(&colData, sizeof(colData));
		colData.mask = LVCF_WIDTH | LVCF_TEXT;
		colData.cx = 260;
		colData.pszText = columnName;

		himlFilesIcons = ImageList_Create(16,16, ILC_COLOR32 | ILC_MASK, 0, 8);

		ListView_InsertColumn(listFiles, 0, &colData);

		int i = 0;

		for (list<wstring>::iterator iter = m_FilesList.begin(); iter != m_FilesList.end(); iter++)
		{
			LVITEM item;
			ZeroMemory(&item, sizeof(item));			

			item.mask = LVIF_TEXT | LVIF_PARAM | LVIF_IMAGE;
			item.iItem = 0;
			item.iImage = i;
			item.lParam = (LPARAM)iter->c_str();

			HICON iconHandle = FilerFrogUtils::GetExtensionSmallIcon(*iter);

			ImageList_AddIcon(himlFilesIcons, iconHandle);	
			iconsList.push_back(iconHandle);

			wchar_t buffer[256];
			wcscpy_s(buffer, 256, FilerFrogUtils::GetFileNameFromPath(*iter).c_str());

			item.pszText = buffer;
			ListView_InsertItem(listFiles, &item);
			i++;
		}

		ListView_SetImageList(listFiles, himlFilesIcons, LVSIL_SMALL);
	}
	void UpdateSortedFilesList()
	{
		m_OutFilesList.clear();

		HWND listFiles = GetDlgItem(IDC_LIST_FILES);

		LVITEM item;
		wchar_t buffer[256];

		int index = -1;

		while ((index = ListView_GetNextItem(listFiles, index, LVNI_ALL)) != -1)
		{
			ZeroMemory(&item, sizeof(item));			
			item.mask = LVIF_TEXT | LVIF_PARAM;
			item.iItem = index;
			item.pszText = buffer;
			item.cchTextMax = 256;

			ListView_GetItem(listFiles, &item);

			wstring filePath = (wchar_t*)item.lParam;
			m_OutFilesList.push_back(filePath);
		}
	}
};
